package org.alex.samples.dao;

import java.util.Collection;
import java.util.List;

import org.alex.samples.model.User;
import org.alex.samples.vo.UserVO;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserDAOImpl extends BaseHibernateDAO<User> implements IUserDAO {
			/*

			User user = new User();

			user.setId(rs.getInt("user_id"));

			user.setName(rs.getString("user_name"));

			user.setAddress(rs.getString("address"));

			user.setBirthday(rs.getDate("birthday"));

			user.setEmail(rs.getString("email"));

			user.setGender(rs.getInt("gender"));

			user.setLast_lock_time(rs.getDate("last_lock_time"));

			user.setLogin_fail_count(rs.getInt("login_fail_count"));

			user.setTel(rs.getString("tel"));*/
	
	
	public List<User> findAllUsers(){
		String hql = " FROM User ";
		List<User> userList = queryForList(hql, null);
		return userList;
	}
	@Override
	public void editUser(User user) throws Exception {
		if(user != null){
			this.update(user);
		}
	}

	@Override
	public void createUser(User user) throws Exception {
		this.save(user);
	}

	@Override
	public void deleleUser(int id) throws Exception {
		User user = this.get(id);
		if(user != null){
			this.delete(user);
		}
	}

	@Override
	public User getUserById(int id){
		User user = this.get(id);
		return user;
	}
	@Override
	public void save(Collection<User> allExcelUser) throws Exception {
		if(allExcelUser == null){
			return;
		}
		for(User user : allExcelUser){
			this.getSession().save(user);
		}
	}
	@Override
	public User getUserByName(String name) throws Exception {
		String hql	 = " FROM User where user_name = ? ";
		return queryForObject(hql, new Object[]{name});
	}
	
	/*

	private static Map<Integer, User> users = new HashMap<Integer,User>();

	

	public UserDAOImpl(){

		users.put(1,new User(1,"alex","man","1999-1","12345678","zhongshan"));

		users.put(2,new User(2,"Jordan","man","1999-3","54456132","shanghai"));

		users.put(3,new User(3,"James","lady","1989-1","12365895","beijing"));

		users.put(4,new User(4,"Kobe","man","1990-10","75963258","guangzhou"));

		users.put(5,new User(5,"Ray","man","1989-6","96325894","Hong Kong"));

		users.put(6,new User(6,"Linux","lady","1986-12","15896472","zhongshan"));

		users.put(7,new User(7,"CentOS","man","1970-1","14521478","beijing"));

	}

	

	@Override

	public List<User> findAllUsers() throws Exception{

		if(users != null){

			return new ArrayList<User>(users.values());

		}else{

		return null;

		}

	}



	@Override

	public User getUserById(int id) throws Exception{

		if(users.containsKey(id)){

			return users.get(id);

		}else{

			return null;

			}

	}



	@Override

	public void editUser(User user) throws Exception{

		if(user != null && users.containsKey(user.getId())){

			users.put(user.getId(), user);

		} else {

			throw new Exception("Update User Fail!");

		}

		

	}



	@Override

	public void createUser(User user)throws Exception{

		if(user != null && !users.containsKey(user.getId())){

			users.put(user.getId(), user);

		}else{

			throw new Exception("Create User Fail!");

		}

	}



	@Override

	public void deleleUser(int id)throws Exception{

		if(users.containsKey(id)){

			users.remove(id);

		} else {

			throw new Exception("Delete User Fail!");

		}

		

	}

	*/
}
